import Taro from '@tarojs/taro';

// custom tabbar height
const tabbarHeight = 54

export interface SystemInfo extends Taro.getSystemInfoSync.Result {
  navBarExtendHeight?: number,
  navBarHeight?: number,
  placeHolderHeight?: number, // iphonex 等机型 安全区域外下方高度
  capsulePosition?: any,
  ios?: boolean,
  tabbarHeight?: number,
  contentHeight?: number
}

export default function getSystemInfo(): SystemInfo {
  let systemInfo: SystemInfo = Taro.getSystemInfoSync() || {
    model: '',
    system: ''
  };
  let ios = !!(systemInfo.system.toLowerCase().search('ios') + 1);
  let rect;
  try {
    rect = Taro.getMenuButtonBoundingClientRect ? Taro.getMenuButtonBoundingClientRect() : null;
    if (rect === null) {
      throw 'getMenuButtonBoundingClientRect error';
    }
    //取值为0的情况  有可能width不为0 top为0的情况
    if (!rect.width || !rect.top || !rect.left || !rect.height) {
      throw 'getMenuButtonBoundingClientRect error';
    }
  } catch (error) {
    let gap = 0; //胶囊按钮上下间距 使导航内容居中
    let width = 88; //胶囊的宽度
    if (systemInfo.platform === 'android') {
      gap = 8;
      width = 88;
    } else if (systemInfo.platform === 'devtools') {
      if (ios) {
        gap = 4; //开发工具中ios手机
      } else {
        gap = 7.5; //开发工具中android和其他手机
      }
    } else {
      gap = 4;
      width = 88;
    }
    if (!systemInfo.statusBarHeight) {
      //开启wifi的情况下修复statusBarHeight值获取不到
      systemInfo.statusBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - 20;
    }
    rect = {
      //获取不到胶囊信息就自定义重置一个
      bottom: systemInfo.statusBarHeight + gap + 32,
      height: 32,
      left: systemInfo.windowWidth - width - 10,
      right: systemInfo.windowWidth - 10,
      top: systemInfo.statusBarHeight + gap,
      width: width
    };
    console.log('error', error);
    console.log('rect', rect);
  }

  let navBarHeight = 0;
  if (!systemInfo.statusBarHeight) {
    //开启wifi和打电话下
    systemInfo.statusBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - 20;
    navBarHeight = (function () {
      let gap = rect.top - systemInfo.statusBarHeight;
      return 2 * gap + rect.height;
    })();

    systemInfo.statusBarHeight = 0;
    systemInfo.navBarExtendHeight = 0; //下方扩展高度
    if (navBarHeight > 40) {
      navBarHeight = 40
    }
  } else {
    navBarHeight = (function () {
      let gap = rect.top - systemInfo.statusBarHeight;
      return 2 * gap + rect.height;
    })();
    if (ios) {
      systemInfo.navBarExtendHeight = 4; //下方扩展4像素高度 防止下方边距太小
    } else {
      systemInfo.navBarExtendHeight = 0;
    }
  }

  systemInfo.navBarHeight = navBarHeight; //导航栏高度不包括statusBarHeight
  systemInfo.capsulePosition = rect; //右上角胶囊按钮信息bottom: 58 height: 32 left: 317 right: 404 top: 26 width: 87 目前发现在大多机型都是固定值 为防止不一样所以会使用动态值来计算nav元素大小
  systemInfo.ios = ios; //是否ios

  systemInfo.placeHolderHeight = 0

  if (systemInfo.safeArea && systemInfo.safeArea.height) {
    systemInfo.placeHolderHeight = systemInfo.screenHeight - systemInfo.safeArea.height - systemInfo.statusBarHeight
  }
  // 不知为何，小米mix2s算出来结果为-28
  if (systemInfo.placeHolderHeight < 0) {
    systemInfo.placeHolderHeight = 0
  }

  systemInfo.tabbarHeight = tabbarHeight

  const {
    screenHeight, 
    statusBarHeight = 0, 
    placeHolderHeight = 0
  } = systemInfo
  const contentHeight = 
  screenHeight - statusBarHeight - placeHolderHeight / 2 - navBarHeight - tabbarHeight

  systemInfo.contentHeight = contentHeight

  return systemInfo;
}